﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace BJ.SharePoint.UserProfileWebPart
{
    [ToolboxItemAttribute(false)]
    public class UserProfileWebPart : WebPart
    {
        /// <summary>
        /// The account name to retrieve the user profile, like DOMAIN\USERNAME 
        /// </summary>
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("User profile account name"), WebDescription("The account name to retrieve the user profile, like DOMAIN\\USERNAME"), Microsoft.SharePoint.WebPartPages.SPWebCategoryName("User Profile Settings")]
        public string AccountName
        {
            get { return _accountName; }
            set { _accountName = value; }
        }
        private string _accountName = "CONTOSO\\aaronp";

        /// <summary>
        /// Retrieves the user profile manager to get user profile for accountname and render table.
        /// </summary>
        protected override void CreateChildControls()
        {
            try
            {
                System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
                ps.Assert();

                Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current;
                UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
                ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;

                UserProfile profile = upm.GetUserProfile(_accountName);

                this.Controls.Add(new Literal() { Text = "<table border='0' > <tr><td>Displayname</td><td>Name</td><td>Value</td></tr>" });
                foreach (ProfileSubtypeProperty prop in pspm.PropertiesWithSection)
                {
                    if (prop.IsSection)
                        this.Controls.Add(new Literal() { Text = string.Format("<tr><td colspan='3'><b>Section: {0}</b></td></tr>", prop.DisplayName) });
                    else
                    {
                        string text = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",
                            prop.DisplayName,
                            prop.Name,
                            profile[prop.Name].Value);

                        this.Controls.Add(new Literal() { Text = text });
                    }
                }
                this.Controls.Add(new Literal() { Text = "</table>" });
            }
            catch (Exception ex)
            {
                this.Controls.Add(new Literal() { Text = ex.ToString() });
            }
            finally
            {
                System.Security.CodeAccessPermission.RevertAssert();
            }
        }
    }
}
